
import pygame
import math
import guiutil


def calc_pos(angle, width, radius):
    x, y = (0, 0)

    if angle == 0:
        x = width / 2 + radius
        y = width / 2
    elif angle <= 45:
        #first quadrant:
        phi = angle
        o = radius * math.sin(math.radians(phi))
        a = radius * math.cos(math.radians(phi))
        x = width / 2 + a
        y = width / 2 - o
    elif angle > 45 and angle < 90:
        phi = 90 - angle
        o = radius * math.sin(math.radians(phi))
        a = radius * math.cos(math.radians(phi))
        x = width / 2 + o
        y = width / 2 - a
    elif angle == 90:
        x = width / 2
        y = width - (width / 2 + radius)
    elif angle > 90 and angle <= 135:
        #second quadrant:
        phi = angle - 90
        o = radius * math.sin(math.radians(phi))
        a = radius * math.cos(math.radians(phi))
        x = width / 2 - o
        y = width / 2 - a
    elif angle > 135 and angle < 180:
        phi = 180 - angle
        o = radius * math.sin(math.radians(phi))
        a = radius * math.cos(math.radians(phi))
        x = width / 2 - a
        y = width / 2 - o
        pass
    elif angle == 180:
        x = width - (width / 2 + radius)
        y = width / 2
    elif angle > 180 and angle <= 235:
        #third quadrant:
        phi = angle - 180
        o = radius * math.sin(math.radians(phi))
        a = radius * math.cos(math.radians(phi))
        x = width / 2 - a
        y = width / 2 + o
    elif angle > 235 and angle < 270:
        phi = 270 - angle
        o = radius * math.sin(math.radians(phi))
        a = radius * math.cos(math.radians(phi))
        x = width / 2 - o
        y = width / 2 + a
    elif angle == 270:
        x = width / 2
        y = width / 2 + radius
    elif angle > 270 and angle <= 305:
        #fourth quadrant:
        phi = angle - 270
        o = radius * math.sin(math.radians(phi))
        a = radius * math.cos(math.radians(phi))
        x = width / 2 + o
        y = width / 2 + a
    elif angle > 305:
        phi = 360 - angle
        o = radius * math.sin(math.radians(phi))
        a = radius * math.cos(math.radians(phi))
        x = width / 2 + a
        y = width / 2 + o
    return (x, y)


def get_color_for_agent(agent_nr):
    if agent_nr == 0:
        return [255, 0, 255]
    elif agent_nr == 1:
        return [255, 0, 0]
    elif agent_nr == 2:
        return [0, 255, 0]
    elif agent_nr == 3:
        return [0, 0, 255]
    elif agent_nr == 4:
        return [100, 100, 100]
    else:
        raise Exception("Agent nr not supported!")


def draw_model(current_model, player_selection):
    width = 600
    radius = 250
    surface = pygame.Surface((width + 200, width))
    surface.fill([255, 255, 255])

    angle_interval = float(360) / len(current_model["state_list"])

    #pygame.draw.circle(surface, [0, 0, 0], (width / 2, width / 2), radius, 1)

    pos_list = []
    for i, state in enumerate(current_model["state_list"]):
        angle = i * angle_interval
        (x, y) = calc_pos(angle, width, radius)
        pos_list.append((x, y))
        #draw state and label:
        lbl = guiutil.draw_label(str(i), 30, 20, False, 0.2, False)
        surface.blit(lbl, (x, y - 20))
        lbl = guiutil.draw_label(state, 150, 20, False, 0.2, False)
        surface.blit(lbl, (x - 50, y + 10))
        pygame.draw.circle(surface, [0, 0, 0], (x, y), 5)

    #draw lines for agents in order:
    if player_selection == -1:
        a = range(5.0)
        list.reverse(a)
        for i in a:
            for transition in current_model["transition_list"]:
                if (transition[2] == i):
                    pygame.draw.line(surface, get_color_for_agent(transition[2]), pos_list[transition[0]], pos_list[transition[1]], (transition[2] * 1) + 2)
    else:
        for transition in current_model["transition_list"]:
            if (transition[2] == player_selection):
                pygame.draw.line(surface, get_color_for_agent(transition[2]), pos_list[transition[0]], pos_list[transition[1]], (transition[2] * 1) + 2)

    for i in range(5):
        pygame.draw.rect(surface, get_color_for_agent(i), [690, 100 + (i * 50), 10, 10])
        lbl = guiutil.draw_label("player %d" % i, 70, 20, False, 0.2, False, font_size = 20)
        surface.blit(lbl, (700, 100 + (i * 50)))

    lbl = guiutil.draw_label("All states are reflexive and all transitions are bidirectional.", 800, 30, False, 0.2, False, font_size = 25)
    surface.blit(lbl, (0, 0))

    return surface
